//登陆模块
layui.define(['config', 'tools'], function (exports) {
    const _user = {};
    const config = layui.config;
    const tools = layui.tools;
    const scope = 'server';
    var authorization = 'Basic ' + (config.authorization || 'cGlnOnBpZw==');

    _user.refreshToken = function (refresh_token) {
        const grant_type = 'refresh_token'
        return tools.request({
            url: '/auth/oauth/token',
            headers: {
                'isToken': false,
                'Authorization': authorization
            },
            method: 'post',
            params: {refresh_token, grant_type, scope}
        })
    }


    //登陆
    _user.loginByUsername = function (username, password, code, randomStr) {
        const grant_type = 'password'
        return tools.request({
            url: '/auth/oauth/token',
            headers: {
                isToken: false,
                'Authorization': authorization
            },
            method: 'post',
            urlParams: {username, password, randomStr, code, grant_type, scope}
        })
    }

    _user.loginByMobile = function (mobile, code) {
        const grant_type = 'mobile'
        return tools.request({
            url: '/auth/mobile/token/sms',
            headers: {
                'Authorization': authorization
            },
            method: 'post',
            params: {mobile: 'SMS@' + mobile, code: code, grant_type}
        })
    }

    _user.loginBySocial = function (state, code) {
        const grant_type = 'mobile'
        return tools.request({
            url: '/auth/mobile/token/social',
            headers: {
                'Authorization': authorization
            },
            method: 'post',
            params: {mobile: state + '@' + code, grant_type}
        })
    }

    _user.getUserInfo = function () {
        return tools.request({
            url: '/plm/user/info',
            method: 'get'
        })
    }

    _user.logout = function () {
        return tools.request({
            url: '/auth/token/logout',
            method: 'delete'
        })
    }

    exports('_user', _user);
});


